
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
</head>
<body>
	<p>明天放假</p>
	<script type="text/javascript">
		var p = document.querySelector('p')

		// p.addEventListener('click', torr, true)
		function addEvent(element, eventType, fn){
				// 传统方式  p.onclick = fn
			// element['on' + eventType] = fn
			console.log(element.attachEvent) //undefined
			console.log(element.addEventListener) //f


			if(element.addEventListener){
				//ie8以下版本不支持
				element.addEventListener(eventType, fn)
			}else if(element.attachEvent){
				// ie8及以下兼容
				element.attachEvent('on' + eventType, fn)
			}else{
				// 传统方式  p.onclick = fn
				// 如果对象点的是个变量，用中括号
				element['on' + eventType] = fn
			}
		}

		addEvent(p, 'click', function(){
			console.log("放假")
		})


		let obj = {
			name:'张三'
		}
		console.log(obj.name)
		let a = 'name'
		console.log(obj.a) //undefined .后面都按字符串处理，不会解析变量
		console.log(obj[a]) //张三  需要解析变量 放中括号里
	</script>
</body>
</html>